/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.enforcement.hid.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.enforcement.common.constant.HidDicConstant;
import com.hopes.bizz.enforcement.hid.dto.RiskHidHiddenDangerDelayDTO;
import com.hopes.bizz.enforcement.hid.entity.RiskHidHiddenDangerDelayEntity;
import com.hopes.bizz.enforcement.hid.service.RiskHidHiddenDangerDelayService;
import com.hopes.bizz.enforcement.hid.vo.RiskHidHiddenDangerDelayVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.boss.common.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 隐患延期管理
 *
 * @author pig
 * @date 2023-07-19 10:14:58
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/hidHidDelay")
@Tag(description = "hidHidDelay", name = "隐患延期管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskHidHiddenDangerDelayController {

    private final RiskHidHiddenDangerDelayService hidHiddenDangerDelayService;


    @Operation(summary = "我的代办", description = "我的代办")
    @GetMapping("/page/todo")
    @PreAuthorize("@pms.hasPermission('risk_hid_delay_view')")
    public R getHidHiddenDangerDelayTodo(Page page, RiskHidHiddenDangerDelayEntity hidHiddenDangerDelay) {
        return R.ok(hidHiddenDangerDelayService.lambdaQuery()
                .eq(RiskHidHiddenDangerDelayEntity::getHandleUserId, SecurityUtils.getUserId())
                .eq(RiskHidHiddenDangerDelayEntity::getHandleStatus, HidDicConstant.RISK_HID_HIDDEN_DANGER_DELAY_UNDISPOSED)
                .eq(ObjectUtil.isNotNull(hidHiddenDangerDelay.getApplyUserId()),RiskHidHiddenDangerDelayEntity::getApplyUserId, hidHiddenDangerDelay.getApplyUserId())
                .page(page)
                .convert(item -> {
                    RiskHidHiddenDangerDelayVO dangerDelayVO = new RiskHidHiddenDangerDelayVO();
                    BeanUtils.copyProperties(item, dangerDelayVO);
                    return dangerDelayVO;
                }));
    }

    @Operation(summary = "我的申请", description = "我的申请")
    @GetMapping("/page/apply")
    @PreAuthorize("@pms.hasPermission('risk_hid_delay_view')")
    public R getHidHiddenDangerDelayMyApply(Page page, RiskHidHiddenDangerDelayEntity hidHiddenDangerDelay) {
        return R.ok(hidHiddenDangerDelayService.lambdaQuery()
                .eq(RiskHidHiddenDangerDelayEntity::getApplyUserId, SecurityUtils.getUserId())
                .eq(ObjectUtil.isNotNull(hidHiddenDangerDelay.getHandleResult()),RiskHidHiddenDangerDelayEntity::getHandleResult, hidHiddenDangerDelay.getHandleResult())
                .page(page)
                .convert(item -> {
                    RiskHidHiddenDangerDelayVO dangerDelayVO = new RiskHidHiddenDangerDelayVO();
                    BeanUtils.copyProperties(item, dangerDelayVO);
                    return dangerDelayVO;
                }));
    }

    @Operation(summary = "我的已办", description = "我的已办")
    @GetMapping("/page/finish")
    @PreAuthorize("@pms.hasPermission('risk_hid_delay_view')")
    public R getHidHiddenDangerDelayFinish(Page page, RiskHidHiddenDangerDelayEntity hidHiddenDangerDelay) {
        return R.ok(hidHiddenDangerDelayService.lambdaQuery()
                .eq(RiskHidHiddenDangerDelayEntity::getHandleUserId, SecurityUtils.getUserId())
                .eq(RiskHidHiddenDangerDelayEntity::getHandleStatus, HidDicConstant.RISK_HID_HIDDEN_DANGER_DELAY_DISPOSE)
                .eq(ObjectUtil.isNotNull(hidHiddenDangerDelay.getApplyUserId()),RiskHidHiddenDangerDelayEntity::getApplyUserId, hidHiddenDangerDelay.getApplyUserId())
                .page(page)
                .convert(item -> {
                    RiskHidHiddenDangerDelayVO dangerDelayVO = new RiskHidHiddenDangerDelayVO();
                    BeanUtils.copyProperties(item, dangerDelayVO);
                    return dangerDelayVO;
                }));
    }


    /**
     * 通过id查询${tableComment}
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('risk_hid_delay_view')")
    public R getById(@PathVariable("id") Long id) {
        RiskHidHiddenDangerDelayEntity dangerDelayEntity = hidHiddenDangerDelayService.getById(id);
        RiskHidHiddenDangerDelayVO riskHidHiddenDangerDelayVO = new RiskHidHiddenDangerDelayVO();
        BeanUtils.copyProperties(dangerDelayEntity, riskHidHiddenDangerDelayVO);
        return R.ok(riskHidHiddenDangerDelayVO);
    }

    /**
     * 新增延期申请
     *
     * @param hidHiddenDangerDelay 新增延期申请
     * @return R
     */
    @Operation(summary = "新增延期申请", description = "新增延期申请")
    @SysLog("新增延期申请")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('risk_hid_delay_add')")
    public R save(@Validated @RequestBody RiskHidHiddenDangerDelayDTO hidHiddenDangerDelay) {
        hidHiddenDangerDelayService.saveDelay(hidHiddenDangerDelay);
        return R.ok();
    }

    /**
     * 修改${tableComment}
     *
     * @param hidHiddenDangerDelay ${tableComment}
     * @return R
     */
    @Operation(summary = "修改${tableComment}", description = "修改${tableComment}")
    @SysLog("修改${tableComment}")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('risk_hid_delay_edit')")
    public R updateById(@RequestBody @Validated RiskHidHiddenDangerDelayEntity hidHiddenDangerDelay) {
        hidHiddenDangerDelayService.updateHidAndDelay(hidHiddenDangerDelay);
        return R.ok();
    }

    /**
     * 通过id删除${tableComment}
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除${tableComment}", description = "通过id删除${tableComment}")
    @SysLog("通过id删除${tableComment}")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('risk_hid_delay_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(hidHiddenDangerDelayService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param hidHiddenDangerDelay 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('risk_hid_delay_export')")
    public List<RiskHidHiddenDangerDelayEntity> export(RiskHidHiddenDangerDelayEntity hidHiddenDangerDelay) {
        return hidHiddenDangerDelayService.list(Wrappers.query(hidHiddenDangerDelay));
    }
}
